package gold.contest;

import gold.utils.InputUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC5981 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public List<Integer> maxScoreIndices(int[] nums) {
            int len = nums.length, maxScore = 0;
            int[] zeroPre = new int[len], oneAfter = new int[len];
            int zeroCount = 0, oneCount = 0;

            for (int i = 0; i < len; i++) {
                zeroCount += nums[i] == 0 ? 1 : 0;
                zeroPre[i] = zeroCount;
            }

            for (int i = len - 1; i >= 0; i--) {
                oneCount += nums[i] == 1 ? 1 : 0;
                oneAfter[i] = oneCount;
            }

            List<Integer> ans = new ArrayList<>();
            for (int i = 0; i <= len; i++) {
                int curPosScore = (0 == i ? 0 : zeroPre[i - 1]) + (len == i ? 0 : oneAfter[i]);
                if (curPosScore > maxScore) {
                    ans.clear();
                    ans.add(i);
                    maxScore = curPosScore;
                } else if (curPosScore == maxScore) {
                    ans.add(i);
                }
            }

            return ans;
        }
    }

    public void run() {
        Solution solution = new Solution();
        System.out.println(solution.maxScoreIndices(InputUtil.toIntegerArray("[0,0,1,0]")));
        System.out.println(solution.maxScoreIndices(InputUtil.toIntegerArray("[0,0,0]")));
        System.out.println(solution.maxScoreIndices(InputUtil.toIntegerArray("[1,1]")));
    }

    public static void main(String[] args) throws Exception {
        LC5981 an = new LC5981();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
